package controllers;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import common.Configuration;


/**
 * @author 	Sami Diab
 * @since 	20/11/2011
 * This is the email paramters that let us send our emails
 */
class SMTPAuthenticator extends Authenticator
{ 
	
	 String _email = Configuration.nEmail;
	 String _password = Configuration.nPassword;
	 String _host = Configuration.nHost;
	 String _port = Configuration.nPort;
	
	 public String getEmail()
	 {
		 return _email;
	 }
	 
	 public String getPassword()
	 {
		 return _password;
	 }
	 
	 public String getHost()
	 {
		 return _host;
	 }
	 
	 public String getPort()
	 {
		 return _port;
	 }
	 
    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(_email, _password);
    }
}

/**
 * @author 	Sami Diab
 * @since 	20/11/2011
 * Contains the function that sends our emails
 */
public class Newsletter_Controller {

	@SuppressWarnings("static-access")
	public boolean sendEmails(String[] to, String displayName, String subject, String html) throws UnsupportedEncodingException
	{
		  // Recipient's email ID needs to be mentioned.
	      //String to = "abcd@gmail.com";

	      // Sender's email ID needs to be mentioned
	      //String from = "web@gmail.com";

	      // Assuming you are sending email from localhost
	      //String host = "localhost";

	      // Get system properties
	      //Properties properties = System.getProperties();
	      //properties.put("mail.smtp.host", "smtp.gmail.com");
		  //properties.put("mail.smtp.port", "465");
		
		  Properties properties = new Properties();
	      SMTPAuthenticator auth = new SMTPAuthenticator();
	      
	      
	      // Setup mail server
	      properties.setProperty("mail.smtp.host", auth.getHost());
	      properties.setProperty("mail.smtp.port", auth.getPort());
	      //properties.setProperty("mail.smtp.user", user);
	      //properties.setProperty("mail.smtp.password", pwd);
	      properties.setProperty("mail.smtp.starttls.enable", "true");
	      properties.setProperty("mail.smtp.debug", "true");
	      properties.setProperty("mail.smtp.auth", "true");
	      //properties.setProperty("mail.smtp.socketFactory.port", port);
	      //properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	      //properties.setProperty("mail.smtp.socketFactory.fallback", "false");
	      

	      // Get the default Session object.
	      Session session = Session.getDefaultInstance(properties, auth);
	      session.setDebug(true);
	      
	      try{
	    	  
	         // Create a default MimeMessage object.
	         MimeMessage message = new MimeMessage(session);

	         // Set From: header field of the header.
	         message.setFrom(new InternetAddress(auth.getEmail(), displayName));

	         // Set To: header field of the header.	         
	         for(int i=0; i< to.length; i++){
	        	 
	        	 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
	         }

	         // Set Subject: header field
	         message.setSubject(subject);

	         // Send the actual HTML message, as big as you like
	         message.setContent(html, "text/html" );

	         // Send message
	         //Transport.send(message);
	         Transport transport = session.getTransport("smtp");
	         //transport.connect(host, user, pwd);
	         transport.send(message);
	         transport.close();
	         
	         System.out.println("Sent message successfully....");
	         
	           
	      }catch (MessagingException mex) {
	    	  
	          mex.printStackTrace();
	    	  return false;
	      }
	      
	      return true;
	}
	
	/*public List<String> getEmails()
	{
		EmailServiceImpl eis = new EmailServiceImpl();
		eis.GetAll();
	}*/
}
